#!/bin/bash

set -x

# Generate an oc image mirror plan file for a given release and destination

v="${VERSION:-v3.11}"
base="${BASE:-quay.io/openshift/origin-}"
tag="${TAG:-${v},${v}.0}"

# use origin images for a given image first
origin_out="$( oc describe is -n origin ${v} | grep 'tagged from' -B1 | grep -E '^[a-z]' )"
for i in $origin_out; do
  a="registry.svc.ci.openshift.org/origin/${v}:${i} "
  for t in ${tag//,/ }; do
    a+=" ${base}${i}:${t}"
  done
  echo $a
done

covered=$( echo ${origin_out} )

# if any ocp images do not occur in origin, mirror them as well
out="$( oc describe is -n ocp ${v} | grep 'tagged from' -B1 | grep -E '^[a-z]' )"
for i in $out; do
  if [[ " ${covered} " == *" $i "* ]]; then
    continue
  fi
  a="registry.svc.ci.openshift.org/ocp/${v}:${i} "
  for t in ${tag//,/ }; do
    a+=" ${base}${i}:${t}"
  done
  echo $a
done
